import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_project/common/route_page/route_name.dart';
import 'package:flutter_project/config/app_bar_view.dart';
import 'package:flutter_project/config/loading_toast.dart';
import 'package:flutter_project/config/theme_color.dart';
import 'package:flutter_project/config/web_page.dart';
import 'package:flutter_project/user/user_login/user_login_view.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../common/privacy/dialog_common_view.dart';
import '../../requst/dio_http_utils.dart';

class UserLoginOff extends StatelessWidget {
  UserLoginOff({super.key});

  final ValueNotifier<bool> isChose = ValueNotifier<bool>(false);

  @override
  Widget build(BuildContext context) {
    return CommonAppWidget.navAppBar(
      title: '申请注销账号',
      childWidget: SingleChildScrollView(
        child: Container(
          color: Colors.white,
          padding: const EdgeInsets.symmetric(horizontal: 10),
          child:
              Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
            const SizedBox(
              height: 10,
            ),
            const Text(
              '为保证你的账号安全，在你提交的注销申请生效前，需要同时满足以下条件',
              style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
            ),
            const SizedBox(
              height: 10,
            ),
            getItem(leftName: '1.账号处于安全状态', content: '账号处于安全使用状态，无被盗风险'),
            const SizedBox(
              height: 20,
            ),
            getItem(
                leftName: '2.账号财产已结清',
                content: '没有财产、欠款、未结清的资金和虚拟权益；本账号及通过本账号接入的第三方中没有未完成或存在争议的服务'),
            const SizedBox(
              height: 20,
            ),
            getItem(leftName: '3.账号权限解除', content: '账号已解除与其他产品的授权登录或绑定关系'),
            const SizedBox(
              height: 20,
            ),
            getItem(leftName: '4.账号无任何纠纷，包括投诉举报', content: ''),
            const SizedBox(
              height: 25,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                ValueListenableBuilder(
                    valueListenable: isChose,
                    builder: (BuildContext context, bool value, Widget? child) {
                      String iconStr = value == false
                          ? 'assets/images/ic_agree_uncheck.png'
                          : 'assets/images/ic_agree_check.png';
                      return GestureDetector(
                        onTap: () {
                          isChose.value = !isChose.value;
                        },
                        child: Image.asset(
                          iconStr,
                          width: 18,
                        ),
                      );
                    }),
                const SizedBox(
                  width: 5,
                ),
                RichText(
                  text: TextSpan(
                      recognizer: TapGestureRecognizer()
                        ..onTap = () {
                          isChose.value = !isChose.value;
                        },
                      text: '我已阅读并同意',
                      style: const TextStyle(color: Colors.black),
                      children: [
                        TextSpan(
                            recognizer: TapGestureRecognizer()
                              ..onTap = () {
                                RouteNames.push(const WebPage(
                                  title: '注销协议',
                                  url: 'assets/html/loginoff.html',
                                ));
                              },
                            text: '“注销协议”',
                            style: TextStyle(color: ThemeColor.mainColor)),
                      ]),
                ),
              ],
            ),
            const SizedBox(
              height: 16,
            ),
            ValueListenableBuilder(
              valueListenable: isChose,
              builder: (BuildContext context, bool value, Widget? child) {
                return GestureDetector(
                  onTap: () {
                    if(isChose.value==true){
                      ConfirmAlertDialog.showView(context,content: '注销账号您的数据将无法还原，确定要注销吗',isShowCancelBtn: true,title: '温馨提示');
                    }else{
                      toast('请先同意注销协议');
                    }

                  },
                  child: Container(
                    margin: const EdgeInsets.symmetric(horizontal: 35),
                    alignment: Alignment.center,
                    height: 45,
                    decoration: BoxDecoration(
                        color: (isChose.value == true)
                            ? ThemeColor.mainColor
                            : const Color(0x803778FD),
                        borderRadius: BorderRadius.circular(25)),
                    child: const Text(
                      '注销',
                      style: TextStyle(
                          fontSize: 14,
                          color: Colors.white,
                          fontWeight: FontWeight.bold),
                    ),
                  ),
                );
              },
            ),
                const SizedBox(
                  height: 20,
                ),
          ]),
        ),
      ),
    );
  }

  Widget getItem({required String leftName, required String content}) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          leftName,
          style: const TextStyle(fontWeight: FontWeight.w500, fontSize: 15),
        ),
        const SizedBox(
          height: 5,
        ),
        Padding(
          padding: const EdgeInsets.only(left: 10),
          child: Text(
            content,
            style: const TextStyle(fontSize: 12),
          ),
        ),
      ],
    );
  }

  //注销账号
  void deleteAccount() async {
    var res = await HttpDioUtils.request('account/delAccount');
    if (res != null && res['ErrCode'] == 0) {
      loginOutAction();
    }
  }
  //清除本地存储数据
  clearLocalStorage() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    sharedPreferences.remove('token');
    sharedPreferences.remove('nickName');
  }

  //退出登录
  loginOutAction() {
    clearLocalStorage();
    RouteNames.openAndRemovedUntilWidget(const UserLoginView());
  }
}
